/*******************************************************************************
 *
 *    Copyright (c) Baina Info Tech Co. Ltd
 *
 *    AndroidTest
 *
 *    RingpullView
 *    TODO File description or class description.
 *
 *    @author: zuochen
 *    @since:  2012-2-2
 *    @version: 1.0
 *
 ******************************************************************************/

package com.sdk.widget;

import com.android.sdk.R;
import com.sdk.util.DisplayManager;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * RingpullView of AndroidTest. 注意：
 * 1.在使用的时候，因为在Y方向上面是可以拉伸的，所以请将android:layout_height属性设置为fill_parent
 * 2.基于第一点，为了不与其他的子视图长生影响，请在RalativeLayout或者FrameLayout中使用.
 *
 * @author zuochen
 */

public class RingpullView extends View {

    // private static final int RINGPULL_WIDTH = DisplayManager.dipToPixel(86);

    private static final int RINGPULL_HEIGHT = DisplayManager.dipToPixel(94);

    // private static final String TAG = RingpullView.class.getSimpleName();

    private static final int REFRESH_RINGPULL_INTERVAL = 20;

    private static final int MSG_REFRESH_RINGPULL_POSITION = 20;

    private static final int PIXEL_RINGPULL_CONTRACT = 20;

    private Bitmap mRingPull;

    private Bitmap mRingpullRod;

    public interface OnRingpullTouchListener {
        void onRingPullTouch();
    }

    private OnRingpullTouchListener mOnRingpullTouchListener;

    public void setOnRingpullTouchListener(OnRingpullTouchListener onRingpullTouchListener) {
        this.mOnRingpullTouchListener = onRingpullTouchListener;
    }

    /**
     * Initiate a new instance of {@link RingpullView}.
     *
     * @param context
     * @param attrs
     */
    public RingpullView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViewData();
    }

    /**
     * Initiate a new instance of {@link RingpullView}.
     *
     * @param context
     */
    public RingpullView(Context context) {
        super(context);
        initViewData();
    }

    /**
     * Initiate a new instance of {@link RingpullView}.
     *
     * @param context
     * @param attrs
     * @param defStyle
     */
    public RingpullView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initViewData();
    }

    private void initViewData() {
        loadViewResource();
    }

    /**
     * 加载会使用到的图片资源
     */
    private void loadViewResource() {
        mRingPull = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.ring);
        mRingpullRod = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.rope2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isMoving) {
            Matrix matrix2 = new Matrix();
            matrix2.postScale(1.0f, ringpullY / mRingpullRod.getHeight());
            matrix2.postTranslate(mRingPull.getWidth() / 2, 0);
            canvas.drawBitmap(mRingpullRod, matrix2, null);
        }
        Matrix matrix = new Matrix();
        matrix.postTranslate(0, ringpullY);
        canvas.drawBitmap(mRingPull, matrix, null);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    float ringpullY = 0;

    /**
     * isMoving 记录是否在拉动
     */
    boolean isMoving = false;

    /**
     * isLegalTouchEvent 当前的touch事情是否响应
     */
    boolean isLegalTouchEvent = false;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float y = event.getY();
        float x = event.getX();
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                if (isLegalTouchEvent) {
                    if (y > getHeight() - RINGPULL_HEIGHT) {
                        return true;
                    }
                    if (y > RINGPULL_HEIGHT) {
                        ringpullY = y;
                    } else {
                        ringpullY = 0;
                    }
                    isMoving = true;
                    invalidate();
                } else {
                    return false;
                }
                break;
            case MotionEvent.ACTION_CANCEL:
                isLegalTouchEvent = false;
                isMoving = false;
                restoreRingPullView();
                break;
            case MotionEvent.ACTION_UP:
                isLegalTouchEvent = false;
                isMoving = false;
                restoreRingPullView();
                break;
            case MotionEvent.ACTION_DOWN:
                if (isLegalTouchEvent(y)) {
                    isLegalTouchEvent = true;
                } else {
                    return false;
                }
                break;
            default:
                break;
        }
        return true;
    }

    private boolean isLegalTouchEvent(float y) {
        if (!isMoving && 0 < y && y < RINGPULL_HEIGHT) {
            return true;
        }
        return false;
    }

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case MSG_REFRESH_RINGPULL_POSITION:
                    if (ringpullY > RINGPULL_HEIGHT) {
                        ringpullY = ringpullY - PIXEL_RINGPULL_CONTRACT;
                        invalidate();
                        mHandler.sendEmptyMessageDelayed(MSG_REFRESH_RINGPULL_POSITION,
                                REFRESH_RINGPULL_INTERVAL);
                    } else {
                        ringpullY = 0;
                        invalidate();
                        mHandler.removeMessages(MSG_REFRESH_RINGPULL_POSITION);
                        isMoving = false;
                        if (null != mOnRingpullTouchListener) {
                            mOnRingpullTouchListener.onRingPullTouch();
                        }
                    }
                    break;

                default:
                    break;
            }
            super.handleMessage(msg);
        }

    };

    /**
     * 还原本视图到初始位置
     */
    private void restoreRingPullView() {
        isMoving = true;
        mHandler.sendEmptyMessage(MSG_REFRESH_RINGPULL_POSITION);
    }
}
